#include <cstdio>

const int Size = 1000 + 10;

struct Node
{
    int se[ Size ], len;
    float sum, avg;
};

int w[ Size ], s[ Size ], father[ Size ], n, r;
Node v[ Size ];

class Heap
{
    public:
        void init( );
        bool empty( );
        int top( );
        void decreaseKey( int num );
        void pop( );
        void push( int num );
    private:
        int array[ Size ], currentSize, place[ Size ];
        void percDown( int hole ), percUp( int hole );
};

Heap h;

void Heap::init( )
{
    currentSize = 0;
}

bool Heap::empty( )
{
    return currentSize == 0;
}

int Heap::top( )
{
    return array[ 1 ];
}

void Heap::percDown( int hole )
{
    int child, tmp = array[ hole ];
    for ( ;hole * 2 <= currentSize; hole = child )
    {
        child = hole * 2;
        if ( child != currentSize && v[ array[ child + 1 ] ].avg > v[ array[ child ] ].avg )
            child++;
        if ( v[ array[ child ] ].avg > v[ tmp ].avg )
        {
            place[ array[ child ] ] = hole;
            array[ hole ] = array[ child ];
        }
        else
            break;
    }
    place[ tmp ] = hole;
    array[ hole ] = tmp;
}

void Heap::percUp( int hole )
{
    int x = array[ hole ];
    for ( ;hole > 1 && v[ x ].avg > v[ array[ hole / 2 ] ].avg; hole /= 2 )
    {
        array[ hole ] = array[ hole / 2 ];
        place[ array[ hole ] ] = hole;
    }
    array[ hole ] = x;
    place[ x ] = hole;
}

void Heap::push( int num )
{
    int hole = ++currentSize;
    place[ num ] = hole;
    array[ hole ] = num;
    percUp( hole );
}

void Heap::pop( )
{
    array[ 1 ] = array[ currentSize-- ];
    place[ array[ 1 ] ] = 1;
    percDown( 1 );
}

void Heap::decreaseKey( int num )
{
    percUp( place[ num ] );
}


int find( int root )
{
    if ( s[ root ] == -1 )
        return root;
    return s[ root ] = find( s[ root ] );
}

void unionset( int root1, int root2 )
{
    s[ root1 ] = root2;
}

void init( )
{
    int i, a, b;
    for ( i = 1; i <= n; i++ )
    {
        s[ i ] = -1;
        scanf("%d", &w[ i ]);
        v[ i ].avg = v[ i ].sum = float( w[ i ] );
        v[ i ].len = 1;
        v[ i ].se[ 0 ] = i;
    }
    for ( i = 0; i < n - 1; i++ )
    {
        scanf("%d%d", &a, &b);
        father[ b ] = a;
    }
    h.init( );
    for ( i = 1; i <= n; i++ )
    {
        if ( i == r )
            continue;
        h.push( i );
    }
}

void work( )
{
    int i, j, v1, v2;
    while ( !h.empty( ) )
    {
        v1 = h.top( );
        h.pop( );
        v2 = find( father[ v1 ] );
        for ( i = v[ v2 ].len; i < v[ v2 ].len + v[ v1 ].len; i++ )
            v[ v2 ].se[ i ] = v[ v1 ].se[ i - v[ v2 ].len ];
        v[ v2 ].len += v[ v1 ].len;
        v[ v2 ].sum += v[ v1 ].sum;
        v[ v2 ].avg = v[ v2 ].sum / v[ v2 ].len;
        if ( v2 != r )
            h.decreaseKey( v2 );
        unionset( v1, v2 );
    }
}

void print( )
{
    int sum = 0, i;
    for ( i = 1; i <= v[ r ].len; i++ )
        sum += w[ v[ r ].se[ i - 1 ] ] * i;
    printf("%d\n", sum);
}

int main( )
{
    while ( scanf("%d%d", &n, &r) != EOF )
    {
        if ( n == 0 && r == 0 )
            break;
        init( );
        work( );
        print( );
    }
}
